<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/5/22
 * Time: 11:26
 */

namespace app\admin\logic;


use app\admin\logic\code\ErrorCode;
use app\common\model\BusBrand;
use think\Error;

class BusBrandLogic extends BaseLogic
{

    private $busBrandModel;

    public function __construct()
    {
        parent::__construct();
        $this->busBrandModel = new BusBrand();
    }

    /**
     * 品牌列表
     * @param $tempWhere
     * @param $nowPage
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function brandList($tempWhere, $nowPage){
        $where = [];
        if ($tempWhere['name']){
            $where[] = ['name', 'LIKE', "%".$tempWhere['name']."%"];
        }
        if ($tempWhere['status']){
            $where[] = ['status', '=', $tempWhere['status']];
        }
        $brandCount = $this->busBrandModel->where($where)->count();
        if ($brandCount > 0){
            $brandList = $this->busBrandModel->where($where)->page($nowPage, self::$pageNum)->select();
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $brandList, $brandCount);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }

    }

    /**
     * 品牌信息
     * @param $id
     * @return mixed
     */
    public function brandInfo($id){
        $info = $this->busBrandModel->get($id);
        if ($info){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $info);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '品牌信息不存在');
        }
    }

    /**
     * 处理品牌逻辑
     * @param $data
     * @return mixed
     */
    public function handle($data){
        $act = $data['act'];
        unset($data['act']);
        $res = false;
        switch ($act){
            case 'add':
                //添加品牌
                $res = $this->busBrandModel->allowField(true)->save($data);
                break;
            case 'edit':
                //编辑品牌
                if (empty($data['icon'])){
                    unset($data['icon']);
                }
                if (!$data['status']){
                    $data['status'] = 0;
                }
                $res = $this->busBrandModel->allowField(true)->save($data,['id' => $data['id']]);
                break;
            case 'del':
                $brandInfo = $this->busBrandModel->get($data['id']);
                $res = $brandInfo->delete();
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }

    /**
     * 获取所有品牌
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllBrand(){
        $where[] = ['status', '=', 1];
        $list = $this->busBrandModel->where($where)->field('id,name')->select();
        if ($list){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $list);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '品牌列表不存在');
        }
    }

}